#pragma once

#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <memory>
#include "Log.hpp"
#include "Mutex.hpp"
#include "Cond.hpp"
#include "Thread.hpp"

namespace ThreadPoolModule
{
    using namespace LogModule;
    using namespace ThreadModule;
    using namespace LockModule;
    using namespace CondModule;

    using thread_t = std::shared_ptr<Thread>;

    const static int defaultnum = 5;
    
    template<typename T>
    class ThreadPool
    {
    private:
        bool IsEmpty() {return _taskq.empty(); }
        void HandlerTask(std::string name)
        {
            LOG(LogLevel::INFO) << "线程:" << name << ", 进入handle逻辑";
            while(true)
            {
                //拿任务
                T t;
                {
                    LockGuard lockguard(_lock);
                    while(IsEmpty() && _isrunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }
                    if(IsEmpty() && !_isrunning)
                        break;
                    t = _taskq.front();
                    _taskq.pop();
                }
                t();
            }
            LOG(LogLevel::INFO) << "线程:" << name << "执行完任务退出";
        }
        ThreadPool(const ThreadPool<T> &) = delete;
        ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;
        ThreadPool(int num = defaultnum)
            : _num(num)
            , _wait_num(0)
            , _isrunning(false)
        {
            for(int i = 0; i < _num; i++)
            {
                _threads.push_back(std::make_shared<Thread>(std::bind(&ThreadPool::HandlerTask, this, std::placeholders::_1)));
                LOG(LogLevel::INFO) << "构建线程" << _threads.back()->Name() << "成功";
            }
        }

    public:
    //获取单例
        static ThreadPool<T> *getInstance()
        {
            if(instance == NULL)
            {
                LockGuard lockguard(mutex); 
                if(instance == NULL)
                {
                    LOG(LogLevel::INFO) << "单例首次执行";
                    instance = new ThreadPool<T>();
                    instance->Start();
                }
            }
            return instance;
        }
        void Start()
        {
            if(_isrunning)
                return ;
            _isrunning = true;
            for(auto &thread_ptr : _threads)
            {
                LOG(LogLevel::INFO) << "启动线程" << thread_ptr->Name() << "成功";
                thread_ptr->Start();
            }
        }
        void Equeue(T in)
        {
            LockGuard lockguard(_lock);
            if(!_isrunning)
                return ;
            _taskq.push(in);
            if(_wait_num > 0)
                _cond.Notify();
        }
        //回收线程
        void wait()
        {
            for (auto &thread_ptr : _threads)
            {
                thread_ptr->Join();
                LOG(LogLevel::INFO) << "回收线程" << thread_ptr->Name() << " ... 成功";
            }
        }

        //停止线程池
        void Stop()
        {
            LockGuard lockguard(_lock);
            if(_isrunning)
            {
                _isrunning = false;
                if(_wait_num > 0)
                    _cond.NotifyAll();
            }
        }
        ~ThreadPool(){}
    private:
        std::vector<thread_t> _threads; //存放线程
        int _num; //线程数量
        int _wait_num; //等待数量
        std::queue<T> _taskq; //临界资源, 任务队列
        Mutex _lock;
        Cond _cond;
        bool _isrunning;
        static ThreadPool<T> *instance;
        static Mutex mutex; //只用来保护单例
    };
    template <typename T>
    ThreadPool<T> *ThreadPool<T>::instance = NULL;
    template <typename T>
    Mutex ThreadPool<T>::mutex; //保护单例
}